﻿using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System;
using System.Linq;

namespace SacNetUG.Models
{
    [MetadataType(typeof(UserMd))]
    public partial class User
    {
        internal class UserMd
        {
            [Required(ErrorMessage = "First Name is Required")]
            [DisplayName("First")]
            public string FirstName { get; set; }
            [Required(ErrorMessage = "Last Name is Required")]
            [DisplayName("Last")]
            public string LastName { get; set; }
            
            [RegularExpression(@"^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$", ErrorMessage = "Email Address is invalid")]
            [DisplayName("Email Address")]
            public string EmailAddress { get; set; }
        }

        public static bool UserInRole(string user, string role)
        {
            var db = new SacNetUGDataContext();
            return (from ur in db.vwUserRoles
                    where ur.AuthID == user &&
                          ur.Name == role
                    select ur).Count() > 0;
        }

        public string Name 
        {
            get
            {
                return string.Format("{0} {1}", FirstName, LastName);
            }
            //set
            //{
            //    //Do nothing - added so SanitizeInput doesn't complain
            //}
        }
    }
}
